package cn.com.css.taxInspection.attention.service.impl;

import cn.com.css.taxInspection.attention.entity.AjglCljdsqcEntity;
import cn.com.css.taxInspection.attention.mapper.AjglCljdsqcMapper;
import cn.com.css.taxInspection.attention.service.AjglCljdsqcService;
import cn.com.css.taxInspection.gt3.entity.DmGySwjgEntity;
import cn.com.css.taxInspection.gt3.entity.DmGySwryEntity;
import cn.com.css.taxInspection.gt3.service.DmGySwjgService;
import cn.com.css.taxInspection.gt3.service.DmGySwryService;
import cn.com.css.taxInspection.system.service.impl.HttpSessionService;
import cn.com.css.taxInspection.util.TsBqUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;

@Service
@Slf4j
public class AjglCljdsqcServiceImpl extends ServiceImpl<AjglCljdsqcMapper, AjglCljdsqcEntity> implements AjglCljdsqcService {


    @Resource
    private DmGySwjgService dmGySwjgService;
    @Resource
    private DmGySwryService dmGySwryService;
    @Resource
    private HttpSessionService httpSessionService;

    @Override
    @DS("oracle")
    public IPage<AjglCljdsqcEntity> queryCljdsTable(AjglCljdsqcEntity ajglCljdsqcEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String swjgDm = httpSessionService.getLogSwjgDm();
        IPage<AjglCljdsqcEntity> page = new Page<>(ajglCljdsqcEntity.getPage(), ajglCljdsqcEntity.getLimit());
        LambdaQueryWrapper<AjglCljdsqcEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AjglCljdsqcEntity::getSwjgDm, swjgDm);
        //案件编号
        if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getJcajbh())) {
            queryWrapper.like(AjglCljdsqcEntity::getJcajbh, ajglCljdsqcEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getNsrsbh())) {
            queryWrapper.like(AjglCljdsqcEntity::getNsrsbh, ajglCljdsqcEntity.getNsrsbh());
        }
        //纳税人识别号List
        if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getNsrsbhList())) {
            queryWrapper.in(AjglCljdsqcEntity::getNsrsbh, ajglCljdsqcEntity.getNsrsbhList());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getNsrmc())) {
            queryWrapper.like(AjglCljdsqcEntity::getNsrmc, ajglCljdsqcEntity.getNsrmc());
        }
        //日期
        if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getStartTime())) {
            queryWrapper.between(AjglCljdsqcEntity::getWszzrq, sdf.parse(ajglCljdsqcEntity.getStartTime()), sdf.parse(ajglCljdsqcEntity.getEndTime()));
        }
        //销号日期
        if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getXhStartTime())) {
            queryWrapper.between(AjglCljdsqcEntity::getXhrq, sdf.parse(ajglCljdsqcEntity.getXhStartTime()), sdf.parse(ajglCljdsqcEntity.getXhEndTime()));
        }
        queryWrapper.orderByDesc(AjglCljdsqcEntity::getXhrq);
        IPage<AjglCljdsqcEntity> resPage = page(page, queryWrapper);
        List<AjglCljdsqcEntity> ajglCljdsqcEntityList = resPage.getRecords();
        for (AjglCljdsqcEntity cljdsqcEntity : ajglCljdsqcEntityList) {
            if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getLrrDm())) {
                DmGySwryEntity dmGySwryEntity = dmGySwryService.getSwry(cljdsqcEntity.getLrrDm());
                String swryxm = dmGySwryEntity.getSwryxm();
                cljdsqcEntity.setLrrDm(cljdsqcEntity.getLrrDm() + "|" + swryxm);
            }
            if (TsBqUtils.isNotNull(cljdsqcEntity.getSwjgDm())) {
                cljdsqcEntity.setSwjgmc(dmGySwjgService.getSwjgDmBySwjgJc(cljdsqcEntity.getSwjgDm()));
            }
        }
        return resPage;
    }

    @Override
    @DS("oracle")
    public List<AjglCljdsqcEntity> exportCljdsTable(AjglCljdsqcEntity ajglCljdsqcEntity) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String swjgDm = httpSessionService.getLogSwjgDm();
        LambdaQueryWrapper<AjglCljdsqcEntity> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AjglCljdsqcEntity::getSwjgDm, swjgDm);
        //案件编号
        if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getJcajbh())) {
            queryWrapper.like(AjglCljdsqcEntity::getJcajbh, ajglCljdsqcEntity.getJcajbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getNsrsbh())) {
            queryWrapper.like(AjglCljdsqcEntity::getNsrsbh, ajglCljdsqcEntity.getNsrsbh());
        }
        //纳税人识别号
        if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getNsrsbhList())) {
            queryWrapper.in(AjglCljdsqcEntity::getNsrsbh, ajglCljdsqcEntity.getNsrsbhList());
        }
        //纳税人名称
        if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getNsrmc())) {
            queryWrapper.like(AjglCljdsqcEntity::getNsrmc, ajglCljdsqcEntity.getNsrmc());
        }
        //日期
        if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getStartTime())) {
            queryWrapper.between(AjglCljdsqcEntity::getWszzrq, sdf.parse(ajglCljdsqcEntity.getStartTime()), sdf.parse(ajglCljdsqcEntity.getEndTime()));
        }
        //销号日期
        if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getXhStartTime())) {
            queryWrapper.between(AjglCljdsqcEntity::getXhrq, sdf.parse(ajglCljdsqcEntity.getXhStartTime()), sdf.parse(ajglCljdsqcEntity.getXhEndTime()));
        }
        queryWrapper.orderByDesc(AjglCljdsqcEntity::getXhrq);
        List<AjglCljdsqcEntity> ajglCljdsqcEntityList = this.list(queryWrapper);
        for (AjglCljdsqcEntity cljdsqcEntity : ajglCljdsqcEntityList) {
            if (TsBqUtils.isNotNull(ajglCljdsqcEntity.getLrrDm())) {
                DmGySwryEntity dmGySwryEntity = dmGySwryService.getSwry(cljdsqcEntity.getLrrDm());
                String swryxm = dmGySwryEntity.getSwryxm();
                cljdsqcEntity.setLrrDm(cljdsqcEntity.getLrrDm() + "|" + swryxm);
            }
            if (TsBqUtils.isNotNull(cljdsqcEntity.getSwjgDm())) {
                cljdsqcEntity.setSwjgmc(dmGySwjgService.getSwjgDmBySwjgJc(cljdsqcEntity.getSwjgDm()));
            }
        }
        return ajglCljdsqcEntityList;
    }
}
